﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.Web.Hosting;
using System.IO;
using System.Web.Mvc.Html;

namespace MultilingualInSpaInMvc
{
    /// <summary>
    /// WidgetHelper classs to generate a widget definition
    /// </summary>
    public static class WidgetHelper
    {
        #region Constructor Method
        #endregion

        #region Fields and Constants

        private const string WidgetsLocation = "~/Widgets/";
        private const string JSInclusionTemplate = "<script src=\"{0}\" type=\"text/javascript\"></script>";
        private const string CSSInclusionTemplate = "<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\" />";

        #endregion

        #region Properties
        #endregion

        #region Public Methods

        /// <summary>
        /// Outputs a widget definition
        /// </summary>
        /// <param name="helper">The helper.</param>
        /// <param name="widgetName">Name of the widget.</param>
        /// <param name="templates">The templates.</param>
        /// <param name="jsModels">The js models.</param>
        /// <param name="cssFiles">The CSS files.</param>
        /// <returns></returns>
        public static MvcHtmlString WidgetDefinition(this HtmlHelper helper, string widgetName, IEnumerable<string> templates, IEnumerable<string> jsFiles, IEnumerable<string> cssFiles)
        {
            StringBuilder widgetDefinition = new StringBuilder();
            string widgetRoot = VirtualPathUtility.AppendTrailingSlash(VirtualPathUtility.Combine(WidgetsLocation, widgetName));
            if (cssFiles != null)
            {
                foreach (string cssFile in cssFiles)
                {
                    widgetDefinition.AppendLine(GenerateCSSDefinition(VirtualPathUtility.Combine(widgetRoot, cssFile)));
                }
            }

            if (jsFiles != null)
            {
                foreach (string jsFile in jsFiles)
                {
                    widgetDefinition.AppendLine(GenerateJSDefinition(VirtualPathUtility.Combine(widgetRoot, jsFile)));
                }
            }

            if (templates != null)
            {
                foreach (string templateFile in templates)
                {
                    widgetDefinition.AppendLine(GenerateTemplateDefinition(helper,VirtualPathUtility.Combine(widgetRoot, templateFile)));
                }
            }
            
            return new MvcHtmlString(widgetDefinition.ToString());
        }

        #endregion

        #region Private Methods


        private static string GenerateTemplateDefinition(HtmlHelper helper, string templateVirtualPath)
        {
            StringBuilder templateStringBuilder = new StringBuilder();
            string templateFile = VirtualPathUtility.ToAbsolute(templateVirtualPath);
            string templateID = Path.GetFileNameWithoutExtension(VirtualPathUtility.GetFileName(templateVirtualPath));

            using (StreamReader sr = new StreamReader(VirtualPathProvider.OpenFile(templateFile)))
            {
                templateStringBuilder.AppendLine(String.Format("<script type=\"text/html\" id=\"{0}\">", templateID));
                //templateStringBuilder.AppendLine(sr.ReadToEnd());
                templateStringBuilder.AppendLine(helper.Partial(templateFile).ToHtmlString());
                templateStringBuilder.AppendLine("</script>");
            }
            return templateStringBuilder.ToString();
        
        }

        private static string GenerateJSDefinition(string jsVirtualPath)
        {
            return string.Format(JSInclusionTemplate, VirtualPathUtility.ToAbsolute(jsVirtualPath));

        }

        private static string GenerateCSSDefinition(string cssVirtualPath)
        {
            return string.Format(CSSInclusionTemplate, VirtualPathUtility.ToAbsolute(cssVirtualPath));
        }

        #endregion
    }
}